<template>
  <view>
    <head-search-view searchHint="单位名称搜索" :is-show-back="true" @search="search"/>
    <view v-for="(item,ind) in list" :key="ind" @click="itemClick(item)">
      <item-unit-view :item="item"/>
    </view>
    <no-data-view :list="list" :load-state="loadState"/>
    <uni-load-more :status="loadState">
    </uni-load-more>
  </view>
</template>

<script lang="ts">
import {Vue, Component} from "vue-property-decorator";
import HeadSearchView from "@/pages/components/head_search_view.vue";
import ItemUnitView from "@/pages/unit/components/item_unit_view.vue";
import XRouterConfig from "@/api/XRouterConfig";
import BaseApi from "@/api/BaseApi";
import ApiDefine from "@/api/ApiDefine";
import {mixins} from "vue-class-component";
import EventConfig from "@/mixins/EventConfig";
import StateConfig from "@/mixins/StateConfig";
import NoDataView from "@/pages/components/no_data_view.vue";

@Component({
  components: {
    HeadSearchView,
    ItemUnitView,
    NoDataView,
  }
})
export default class UnitView extends mixins(Vue, EventConfig, StateConfig) {
  created() {
    this.initData();
  //   this.toEvent('onReachBottom', () => {
		// console.log('bottom')
  //     this.page++;
  //     this.initData();
  //   })
  }

  search(value: any) {
    this.page = 1;
    this.unitname = value;
    this.initData();
  }

  page = 1;
  pageRow = 10;
  unitname = ''

  list: any = [];
  loadState = 'more';

  async initData() {
    this.loadState = 'loading'
    let value = await BaseApi.instance().fetch(ApiDefine.UNIT_LIST_URL, {
      sysuserid: uni.getStorageSync('userId'),
      unitname: this.unitname,
      page: this.page,
      pageRow: this.pageRow,
    })
    if (value == null) {
      // this.setDataState(this.list)
      return;
    }
    if (value.data ?? [].length == 0) {
      this.loadState = 'no-more'
    }
    if (this.page == 1) {
      this.list = value.data ?? []
      // this.setDataState(this.list)
      return;
    }
    this.list = this.list.concat(value.data ?? []);
    // this.setDataState(this.list)
  }
  onReachBottom() {
    this.page++;
    this.initData();
  }
  itemClick(item: any) {
    XRouterConfig.goTo('unit_details', {osid: item.osid})
  }
}
</script>

<style scoped>

</style>